package com.example.comm.comm;

import org.apache.log4j.Logger;
import org.springframework.data.domain.Page;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class BaseAction {
	
	private Logger log = Logger.getLogger(BaseAction.class);
	
	public int getRows(HttpServletRequest req) {
		try {
			int rows = Integer.valueOf(req.getParameter("rows"));
			return rows;
		} catch(Exception e) {
			log.error("未设置pagesize");
		}
		return -1;
	}
	
	public int getPage(HttpServletRequest req) {
		try {
			int page = Integer.valueOf(req.getParameter("page"));
			return page;
		} catch(Exception e) {
			log.error("未设置页面下标");
		}
		return -1;
	}
	
	public void outBack(HttpServletResponse resp, String data, String type) {
		resp.setCharacterEncoding("utf-8");
		resp.setContentType(type);
		try {
			resp.getWriter().print(data);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public <T> PageVO<T> pageToVO(Page<T> p) {
		PageVO<T> vp = new PageVO<T>();
		vp.setContentList(p.getContent());
		vp.setCurrPage(p.getNumber()+1);
		vp.setTotalPages(p.getTotalPages());
		vp.setTotals(p.getTotalElements());
		return vp;
	}

}
